package gw.com.android.ui.quote2.widget;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.gwtsz.gts2.hx.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import gw.com.android.app.AppContances;
import gw.com.android.app.AppMain;
import gw.com.android.app.GTConfig;
import gw.com.android.ui.views.FitPopupWindow;
import gw.com.android.utils.ColorThemeUtil;
import gw.com.jni.library.terminal.GTSConst;
import www.com.library.model.DataItemDetail;
import www.com.library.model.DataItemResult;
import www.com.library.util.CommonUtils;
import www.com.library.util.DeviceUtil;
import www.com.library.view.RecyclerClickListener;
import www.com.library.view.TintImageTextView;

/**
 * Created by reeta.zou on 2016/10/19.
 * 行情界面的一些菜单弹层
 */
public class QuoteMenuPopWindow {
    public Activity context;
    private View contentView;
    private FitPopupWindow mPopupWindow;
    private DataItemResult mArray;
    private DataItemDetail mTickModel;
    protected RecyclerClickListener callback;// 点击事件的回调函数
    protected RecyclerView.Adapter modeListAdapter;
    protected RecyclerView popListView;// pop上的listview

    public QuoteMenuPopWindow(Activity activity, DataItemDetail mTick, RecyclerClickListener callback) {
        refreshData(mTick);
        this.context = activity;
        this.callback = callback;
        LayoutInflater inflater = LayoutInflater.from(context);
        contentView = inflater.inflate(R.layout.dialog_chart_menu, null);
        createPopWindow(contentView);
    }

    public void createPopWindow(View dialogView) {
//        dialogView.setBackgroundResource(mBgID);
        popListView = (RecyclerView) dialogView.findViewById(R.id.pop_list);
//        popListView.addItemDecoration(new RecycleViewDivider(
//                context, LinearLayoutManager.VERTICAL, R.drawable.common_divider_left10_bg, false));//分割线？
        modeListAdapter = new DataAdapter(context);
        popListView.setAdapter(modeListAdapter);
        // 线性布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        // 设置布局管理器
        popListView.setLayoutManager(layoutManager);

    }

    /**
     * 刷新界面数据
     *
     * @param mTick
     */
    public void refreshData(DataItemDetail mTick) {
        this.mTickModel = mTick;
        if (mArray == null) {
            mArray = new DataItemResult();
        } else {
            mArray.clear();
        }

        DataItemDetail itemDetail = null;
        if (mTickModel.getInt(GTSConst.JSON_KEY_ZONE) != GTSConst.ZONE_TYPE_REF
                && mTickModel.getInt(GTSConst.JSON_KEY_TRADESTATE) != AppContances.TRADE_STATU_REF) {
            itemDetail = new DataItemDetail();
            itemDetail.setIntValue("title", R.string.quote_menu_buy);
            itemDetail.setIntValue("imageID", R.mipmap.a_quote_popbuy);
            itemDetail.setBooleanValue("isEnable", true);
            mArray.addItem(itemDetail);

            itemDetail = new DataItemDetail();
            itemDetail.setIntValue("title", R.string.quote_menu_sell);
            itemDetail.setIntValue("imageID", R.mipmap.a_quote_popsell);
            itemDetail.setBooleanValue("isEnable", true);
            mArray.addItem(itemDetail);

            if (GTConfig.instance().getAccountType() != GTConfig.ACC_TYPE_GUEST) {
                itemDetail = new DataItemDetail();
                itemDetail.setIntValue("title", R.string.quote_menu_property);
                itemDetail.setIntValue("imageID", R.mipmap.a_quote_popproperty);
                itemDetail.setBooleanValue("isEnable", true);
                mArray.addItem(itemDetail);
            }
        }

        itemDetail = new DataItemDetail();
        if (mTickModel.getBoolean(GTSConst.JSON_KEY_ISSELECTED)) {
            itemDetail.setIntValue("title", R.string.quote_menu_self_cancel);
            itemDetail.setIntValue("imageID", R.mipmap.a_quote_popdel);
        } else {
            itemDetail.setIntValue("title", R.string.quote_menu_self_add);
            itemDetail.setIntValue("imageID", R.mipmap.a_quote_popadd);
        }
        itemDetail.setBooleanValue("isEnable", true);
        mArray.addItem(itemDetail);

        if (null != modeListAdapter) {
            modeListAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 弹出自适应位置的popupwindow
     *
     * @param anchorView 目标view
     */
    public void showPopup(View anchorView) {
        if (mPopupWindow == null) {
            mPopupWindow = new FitPopupWindow(context,
                    DeviceUtil.instance().getScreenPixelsWidth(context) - DeviceUtil.instance().dip2px(2, context),
                    ViewGroup.LayoutParams.WRAP_CONTENT
            );
        }

        mPopupWindow.setView(contentView, anchorView);
        if (hasShowing() || modeListAdapter.getItemCount() < 1) {
            return;
        }
        mPopupWindow.show();
    }

    /**
     * 返回当前pop是否显示状态
     *
     * @author jay
     */
    public boolean hasShowing() {
        return null == mPopupWindow ? false : mPopupWindow.isShowing();
    }

    /**
     * 隐藏pop界面
     *
     * @author reeta
     */
    public void hidden() {


        if (null == mPopupWindow) {
            return;
        }
        backgroundAlpha(context, 1f);
        mPopupWindow.dismiss();
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        if (bgAlpha == 1) {
            context.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        context.getWindow().setAttributes(lp);
    }

    /**
     * 列表适配器
     *
     * @author reeta
     */
    public class DataAdapter extends RecyclerView.Adapter {
        private LayoutInflater mInflater;

        public DataAdapter(Activity act) {
            mInflater = LayoutInflater.from(act);
        }

        public DataItemDetail getItem(int position) {
            if (position < 0 || position >= mArray.getDataCount())
                return null;
            return mArray.getItem(position);
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = mInflater.inflate(R.layout.list_item_quote_menu_pop, parent, false);
            return new ItemView(itemView);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ItemView viewHolder = (ItemView) holder;
            if (viewHolder.mView != null) {
                DataItemDetail mItem = getItem(position);
                viewHolder.mView.setText(AppMain.getAppString(mItem.getInt("title")));
                viewHolder.mView.setImageResource(mItem.getInt("imageID"));
                if (mItem.getBoolean("isEnable")) {
                    viewHolder.mView.setImageColorValue(ColorThemeUtil.instance().color_k, 0);
                    viewHolder.mView.setTextColorValue(ColorThemeUtil.instance().color_b, 0);
                } else {
                    viewHolder.mView.setImageColorValue(ColorThemeUtil.instance().color_h, 0);
                    viewHolder.mView.setTextColorValue(ColorThemeUtil.instance().color_h, 0);
                }
                viewHolder.mView.setEnabled(mItem.getBoolean("isEnable"));
                viewHolder.mView.setTag(position);
            }
            //最后一个item不显示分割线
            if (position >= getItemCount() - 1) {
                viewHolder.vDivider.setVisibility(View.GONE);
            } else {
                viewHolder.vDivider.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemCount() {
            if (null != mArray) {
                return mArray.getDataCount();
            }
            return 0;
        }

        class ItemView extends RecyclerView.ViewHolder {

            @BindView(R.id.item_title)
            TintImageTextView mView;
            @BindView(R.id.v_divider)
            View vDivider;

            public ItemView(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }

            @OnClick(R.id.item_layout)
            void onClick() {
                if (CommonUtils.isFastDoubleClick()) {
                    return;
                }
                if (null != callback) {
                    int position = (int) mView.getTag();
                    hidden();
                    mPopupWindow.setFocusable(false);
                    mPopupWindow.update();
                    callback.onClick(position, mArray.getItem(position));
                }
            }
        }
    }
}
